/*
 * BtnUpdate.java
 *
 * Created on June 23, 2007, 4:02 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package latihanibatis.ui.event;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JTable;
import javax.swing.JTextField;
import latihanibatis.dao.MahasiswaDao;
import latihanibatis.ui.MhsTabelModel;
import latihanibatis.ui.PanelStatus;

/**
 *
 * @author javamaniac
 */
public class BtnUpdate extends AbstractAction {
    
    private final javax.swing.JTextField txtAlamat;
    private final javax.swing.JTextField txtKeterangan;
    private final javax.swing.JTextField txtKota;
    private final javax.swing.JFormattedTextField txtNIM;
    private final javax.swing.JTextField txtNama;
    private final PanelStatus status;
    private final JTable table;
    private final MhsTabelModel tableModel;
    private final JButton btnCari;
    private final JButton btnUpdate;
    private final JButton btnSimpan;
    private final JButton btnEdit;
    private final JButton btnDelete;
    private final MahasiswaDao mhsDao;
    
    /** Creates a new instance of BtnUpdate */
    public BtnUpdate(JFormattedTextField txt1, JTextField txt2, JTextField txt3,
        JTextField txt4, JTextField txt5, PanelStatus stat, JTable tbl,
        MhsTabelModel tblMdl, JButton btnCr, JButton btnUpt, JButton btnSmp,
        JButton btnEdit, JButton btnDel, MahasiswaDao dao) {
        
        super();
        this.txtNIM         = txt1;
        this.txtNama        = txt2;
        this.txtKota        = txt3;
        this.txtAlamat      = txt4;
        this.txtKeterangan  = txt5;
        this.status         = stat;
        this.table          = tbl;
        this.tableModel     = tblMdl;
        this.btnCari        = btnCr;
        this.btnUpdate      = btnUpt;
        this.btnSimpan      = btnSmp;
        this.btnEdit        = btnEdit;
        this.btnDelete      = btnDel;
        this.mhsDao         = dao;
    }
    
    public void actionPerformed(final ActionEvent evt) {
        Map<Object, Object> prmUpdate = new HashMap<Object, Object>();
        
        try {
            final int intNim = ((Number)txtNIM.getValue()).intValue();
            final String nama = txtNama.getText().toUpperCase();
            final String kota = txtKota.getText().toUpperCase();
            final String alamat = txtAlamat.getText().toUpperCase();
            final String keter  = txtKeterangan.getText().toUpperCase();
            
            if (intNim <= 0 || nama.trim().length() <= 0 ||
                kota.trim().length() <= 0 || alamat.trim().length() <= 0) {
                status.setStatus("Maap Kolom Isian Bertanda * Tidak Boleh Kosong !!", "ERROR");
            } else {
                final int baris = tableModel.getBarisForUpdate();
                final int kolom = table.getColumnCount();
                
                prmUpdate.put("newNama", nama);
                prmUpdate.put("newKota", kota);
                prmUpdate.put("newAlamat", alamat);
                prmUpdate.put("newNote", keter);
                prmUpdate.put("nim", intNim);
                
                // Update to DataBase
                mhsDao.updateMhs(prmUpdate);
                // Update on Table
                tableModel.updateMhs(nama, alamat, kota, keter, baris, kolom);
                
                prmUpdate.clear();
                
                txtNIM.setEnabled(true);
                txtNIM.setValue(0);
                txtNama.setText("");
                txtKota.setText("");
                txtAlamat.setText("");
                txtKeterangan.setText("");
                
                btnCari.setEnabled(true);
                btnSimpan.setEnabled(true);
                btnEdit.setEnabled(true);
                btnDelete.setEnabled(true);
                
                btnUpdate.setEnabled(false);
                
                status.setStatus("Data Mahasiswa Sudah Terupdate !!", "SUKSES");
                
            } // End else
        } catch (Exception except) {
            status.setStatus("Data Mahasiswa Gagal Ter-udpate !!", "ERROR");
        }
    }
}
